package com.wlyuan.index.domain.source.valueobj;

import com.wlyuan.core.marker.INamed;
import com.wlyuan.core.marker.NamedMap;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;

/**
 * @author yuanjie
 */
@AllArgsConstructor
@Getter
public enum WaybillStatus implements INamed<Integer> {
    WAIT_DISPATCH(10, "待调度", false, false),
    DISPATCHED(20, "已委托", false, false),
    WAIT_ACCEPT(21, "待接收", false, false),
    ACCEPTING(22, "接收中", false, false),
    WAIT_SIGN(30, "待签收", true, true),
    SIGNING(31, "签收中", true, true),
    WAIT_RECEIPT(40, "待回单", true, true),
    RECEIPTED(50, "已回单", true, true),
    CLOSED(60, "已关闭", false, false),
    @Deprecated
    DISCARDED(61, "已废弃", false, false),
    DIVIDED(-1, "已拆单", false, false);


    private final Integer value;
    private final String name;
    private final boolean canQuote;
    private final boolean canBill;

    private static final Map<Integer, WaybillStatus> STATUSES = new NamedMap<>(WaybillStatus.values());

    public static WaybillStatus valueOf(Integer value) {
        return STATUSES.get(value);
    }

    public static boolean isCanQuote(Integer value) {
        return Optional.ofNullable(value)
                .map(WaybillStatus::valueOf)
                .filter(WaybillStatus::isCanQuote)
                .isPresent();
    }

    public static boolean isCanBill(Integer value) {
        return Optional.ofNullable(value)
                .map(WaybillStatus::valueOf)
                .filter(WaybillStatus::isCanBill)
                .isPresent();
    }
}
